<?php

/**
 * FileName: InfoController.php
 * Author: @163.com
 * DateTime: 2023-02-21 下午 09:54
 * Summary:
 */


namespace App\Http\Controllers\Api\User;


use App\Http\Controllers\Api\BasicController;
use App\Models\Dynamic\Lists;
use App\Models\OrderAppoint;
use App\Models\SiteCode;
use App\Models\User;
use App\Models\UserFocus;
use App\Models\UserInfo;
use App\Services\Sms;
use GatewayWorker\Lib\Gateway;
use Jiannei\Response\Laravel\Support\Facades\Response;

class InfoController extends BasicController
{

    /**
     * @api {get} /user/info/basic  基础信息
     * @apiName UserInfoBasic
     * @apiGroup UserInfo
     * @apiDescription 基础信息
     *
     * @apiHeader {String} Authorization='Bearer '
     */
    public function basic()
    {
        $uid = $this->uid();
        $result = (new User())->getUser(['id'=>$uid]);
        if($result->extend){
            $result->extend->comment_num=OrderAppoint::where(['appoint_id'=>$uid,'status'=>5])->count();
            $result->extend->post_num=Lists::where(['user_id'=>$uid])->count();
            $result->extend->text_price_range=0;
            $result->extend->voice_price_range=0;

        }
        return Response::success($result);
    }

    /**
     * @api {post} /user/info  基础信息
     * @apiName UserInfoBasic
     * @apiGroup UserInfo
     * @apiDescription 基础信息
     *
     * @apiHeader {String} Authorization='Bearer '
     */
    public function info()
    {
        $uid = $this->request->input('uid');
        $result = (new User())->getUser(['id'=>$uid]);
        if($result->extend){
            $result->extend->comment_num=OrderAppoint::where(['appoint_id'=>$uid,'status'=>5])->count();
            $result->extend->post_num=Lists::where(['user_id'=>$uid])->count();
        }
        $result->is_focus=User::getFocusStatus($this->uid(),$uid);
        $result->fans_num=UserFocus::where('focus_id',$uid)->count();
        $result->focus_num=UserFocus::where('user_id',$uid)->count();
        return Response::success($result);
    }

    /**
     * @api {post} /user/info/update 更新资料
     * @apiName UserInfoUpdate
     * @apiGroup UserInfo
     * @apiDescription 更新资料
     *
     * @apiHeader {String} Authorization='Bearer '
     *
     * @apiParam {String} avatar 头像
     * @apiParam {String} nickname 昵称
     * @apiParam {String} gender 性别
     * @apiParam {String} birthday 生日
     */
    public function update()
    {
        $data = $this->data(['avatar', 'name', 'gender', 'birthday','bio','intro','city']);
        $where['id'] = $this->uid();
        $result = User::where($where)->update($data);
        $user=auth()->user();
        if($user->xinli==1 || $user->zhanbu==1 || $user->qingting ==1){
            $data =$this->data(['consult_types','text_price','voice_price','take_order_day_begin',
                'take_order_day_end','is_take_order','skill','therapy','listen','technical','education','age_limit','professional','wallet_account','time_limits']);
//            if(!empty($data['text_price']) || !empty($data['voice_price'])){
//                return Response::fail('暂不支持自定义价格');
//            }
            $userinfo=UserInfo::where(['uid'=>$this->uid()])->first();
            if(empty($userinfo)){
                $data['uid']=$this->uid();
                UserInfo::create($data);
            }else{
                UserInfo::where(['uid'=>$this->uid()])->update($data);
            }
        }
        return Response::success($result,'修改成功');
    }

    /**
     * @api {post} /user/info/mobile 更新手机
     * @apiName UserInfoMobile
     * @apiGroup UserInfo
     * @apiDescription 更新手机
     *
     * @apiHeader {String} Authorization='Bearer '
     *
     * @apiParam {String} code 验证码
     * @apiParam {String} mobile 手机号
     * @apiParam {String} code_new 新验证码
     */
    public function mobile()
    {
        $user = (new User())->getDetail($this->uid());
        $data = $this->data(['code', 'mobile', 'code_new']);
        (new Sms())->check($user->mobile, $data['code'], SiteCode::CODE_CHANGE);
        (new Sms())->check($data['mobile'], $data['code_new'], SiteCode::CODE_CHANGE);
        $where['uid'] = $this->uid();
        $update['mobile'] = $data['mobile'];
        $result = User::where($where)->update($update);
        return Response::success($result);
    }

    /**
     * @api {post} /user/info/location 位置更新
     * @apiName UserInfoLocation
     * @apiGroup UserInfo
     * @apiDescription 位置更新
     *
     * @apiHeader {String} Authorization='Bearer '
     *
     * @apiParam {String} longitude  经度
     * @apiParam {String} latitude 纬度
     */
    public function location()
    {
        $data = $this->data(['longitude', 'latitude']);
        $where['uid'] = $this->uid();
        $result = UserInfo::where($where)->update($data);
        return Response::success($result);
    }

    /**
     * @api {post} /user/info/client 绑定ID
     * @apiName UserInfoClient
     * @apiGroup UserInfo
     *
     * @apiHeader {String} Authorization='Bearer '
     *
     * @apiParam {String} client_id ID
     * @apiParam {number} [identity=1] 身份1=用戶2=商家
     */
    public function client()
    {
        $identity = $this->request->input('identity', 1);
        $client_id = $this->request->input('client_id');
        $where['uid'] = $uid = $this->uid();
        $result = UserInfo::where($where)->update(['client_id' => $client_id]);
        if ($identity == 1)
            Gateway::bindUid($client_id, $uid);
        if ($identity == 2)
            Gateway::bindUid($client_id, 'S' . $this->sid());
        return Response::success($result);
    }

    /**
     * @api {get} /user/info/level 升级状态
     * @apiName UserInfoLevel
     * @apiGroup UserInfo
     *
     * @apiHeader {String} Authorization='Bearer '
     *
     */
    public function level()
    {
        $result['integral'] = $integral = UserInfo::where('uid', $this->uid())->value('integral');
        $result['nowLevel'] = $nowLevel = (new MarketLevel())->maxLevel($integral);
        $result['nextLevel'] = $nextLevel = (new MarketLevel())->nextLevel($integral);
        $result['level_all'] = $nextLevel->amount - $nowLevel->amount;
        $result['level_now'] = $integral - $nowLevel->amount;
        $result['level_need'] = $nextLevel->amount - $integral;
        return Response::success($result);

    }

    public function priceRange(){
        $uid=$this->request->input('uid');
        $range=config("teacher.price_range");
        foreach ($range as $k=>$p){


        }


    }

}
